#define _CRT_SECURE_NO_WARNINGS 1

#include"Queue.h"

void QueueInit(LinkQueue* Q)
{
	assert(Q);
	Q->front = NULL;
	Q->rear = NULL;
}

int QueueEmpty(LinkQueue* Q)
{
	if (Q->front == NULL)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void QueueDestroy(LinkQueue* Q)
{
	assert(Q);
	QueueNode* cur = Q->front;
	while (cur)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
	Q->front = NULL;
	Q->rear = NULL;
}

void QueuePush(LinkQueue* Q, QDataType x)
{
	assert(Q);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		printf("%s\n", strerror(errno));
	}
	newnode->data = x;
	newnode->next = NULL;
	if (Q->front == NULL)
	{
		Q->front = newnode;
		Q->rear = newnode;
	}
	else
	{
		Q->rear->next = newnode;
		Q->rear = Q->rear->next;
	}
}

void QueuePop(LinkQueue* Q)
{
	assert(Q != NULL && Q->front != NULL);
	QueueNode* temp = Q->front;
	Q->front = Q->front->next;
	free(temp);
	if (Q->front == NULL)
	{
		Q->rear = NULL;
	}
}

QDataType QueueFront(LinkQueue* Q)
{
	assert(Q != NULL && Q->front != NULL);
	return Q->front->data;
}